<template>
  <el-dialog @close="closeDialog" title="选择用户" :visible.sync="showUser" width="70%" append-to-body close-on-press-escape>
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="客户名字" prop="khname">
        <el-input
          v-model="queryParams.khname"
          placeholder="请输入客户名字"
          clearable
          @input="inputQuery"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="地址" prop="dz">
        <el-input
          v-model="queryParams.dz"
          placeholder="请输入地址"
          clearable
          @input="inputQuery"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
        <el-button icon="el-icon-plus" type="success" size="mini" @click="addUser">新增客户</el-button>
      </el-form-item>
    </el-form>
    <el-table v-loading="loading" :data="khxxList" @selection-change="handleSelectionChange" @row-click="selectData">
      <el-table-column label="客户编号" align="center" prop="bh" width="120" />
      <el-table-column label="客户名字" align="left" prop="khname" />
      <el-table-column label="地址" align="left" prop="dz" />
      <el-table-column label="税号" align="left" prop="sh" width="200" />
    </el-table>
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
    <el-dialog :title="title" :visible.sync="open" @close="cancel" width="60%" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-row>
          <el-col :span="12">
            <el-form-item label="客户名字" prop="khname">
              <el-input v-model="form.khname" placeholder="请输入客户名字" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="地址" prop="dz">
              <el-input v-model="form.dz" placeholder="请输入地址" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="税号" prop="sh">
              <el-input v-model="form.sh" placeholder="请输入税号" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="银行卡号" prop="xhkh">
              <el-input v-model="form.xhkh" placeholder="请输入银行卡号" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="电话" prop="dh">
              <el-input v-model="form.dh" type="number" placeholder="请输入电话" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="排序" prop="px">
              <el-input v-model="form.px" type="number" placeholder="请输入排序" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="录入人" prop="createBy">
              <el-input v-model="form.createBy" readonly />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="录入时间" prop="createTime">
              <el-input v-model="form.createTime" readonly />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item label="备注" prop="bz">
              <el-input
                type="textarea"
                maxlength="1000"
                :autosize="{ minRows: 3, maxRows: 4 }"
                show-word-limit
                placeholder="请输入内容"
                v-model="form.bz">
              </el-input>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button :loading="buttonLoading" type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </el-dialog>
</template>

<script>
import { listKhxx, addKhxx, getMaxBh, updateKhxx } from '@/api/gd/khxx'
import { parseTime } from '@/utils/ruoyi'

export default {
  name: "Khxx",
  props: {
    showUser: {
      type: Boolean
    }
  },
  data() {
    return {
      open: false,
      // 按钮loading
      buttonLoading: false,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 客户信息表格数据
      khxxList: [],
      // 弹出层标题
      title: "",
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        khname: undefined,
        dz: undefined,
        sh: undefined,
        xhkh: undefined,
        px: undefined,
        bz: undefined,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        khname: [
          { required: true, message: "客户名字不能为空", trigger: "blur" }
        ],
        dz: [
          { required: true, message: "地址不能为空", trigger: "blur" }
        ]
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    inputQuery() {
      this.getList();
    },
    closeDialog() {
      this.$emit("closeDialog")
    },
    selectData(row) {
      this.$emit("selectData", row)
    },
    /** 查询客户信息列表 */
    getList() {
      this.loading = true;
      listKhxx(this.queryParams).then(response => {
        this.khxxList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: undefined,
        khname: undefined,
        dz: undefined,
        sh: undefined,
        xhkh: undefined,
        px: undefined,
        bz: undefined,
        createBy: this.$store.getters.nickName,
        createTime: parseTime(new Date()),
        updateBy: undefined,
        updateTime: undefined
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      // this.resetForm("queryParams");
      this.queryParams = {
          pageNum: 1,
          pageSize: 10,
          khname: undefined,
          dz: undefined,
          sh: undefined,
          xhkh: undefined,
          px: undefined,
          bz: undefined,
      }
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    // 新
    async addUser() {
      this.reset()
      // 新增的时候添加用户默认排序
      let bh = await getMaxBh()
      this.$set(this.form, 'px', bh)
      this.open = true;
      this.title = "添加客户信息";
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.buttonLoading = true;
          this.form.status = '0'  // 新增，或者修改，则将状态改为0
          addKhxx(this.form).then(response => {
            this.$modal.msgSuccess("新增成功");
            this.open = false;
            this.getList();
          }).finally(() => {
            this.buttonLoading = false;
          });
        }
      });
    },
  }
};
</script>
